/**
*作者 ：王文成
*日期:2011-03-07
*Email: wellse@qq.com
*/

// 去除前后空格
String.prototype.trim = function (){
	return this.replace(/(^\s*)|(\s*$)/g,"");
};

// 返回字符串实际长度,汉字2个字符
String.prototype.size = function(){
	return this.replace(/[^\x00-\xff]/g,"aa").length;
};

// 首字母小写
String.prototype.lowerCaseFirstChar = function() {
	if( this.length > 0 )
		return this.charAt(0).toLowerCase() + this.substring(1,this.length);
	return this;
};

// 首字母大写
String.prototype.upperCaseFirstChar = function(){
	if( this.length > 0 )
		return this.charAt(0).toUpperCase() + this.substring(1,this.length);
	return this;
};

// 是否以字符串开始
String.prototype.startWith = function( value ){
	return this.indexOf(value) == 0;
};

// 返回Hash编码
String.prototype.hashCode = function(){
	var hash = 0;
	for( var i = 0 ; i < this.length ; i++ ){
		hash += this.charCodeAt(i);
	}
	return hash;
};

//扩展Date
/** Adds the number of days array to the Date object. */  
Date._MD = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);   
  
/** Constants used for time computations */  
Date.SECOND = 1000 /* milliseconds */;   
Date.MINUTE = 60 * Date.SECOND;   
Date.HOUR = 60 * Date.MINUTE;   
Date.DAY = 24 * Date.HOUR;   
Date.WEEK = 7 * Date.DAY;   

/**  
 * 扩展Date的toString方法用于格式化。测试 var a = new Date(); alert(a.toString("yyyy-MM-dd HH mm ss"));  
 */  
Date.prototype.toString = function() {   
    if (arguments.length > 0) {   
        var formatStr = arguments[0];   
        var str = formatStr;   
  
        str = str.replace(/yyyy|YYYY/, this.getFullYear());   
        str = str.replace(/yy|YY/, (this.getFullYear() % 100) > 9 ? (this  
                .getFullYear() % 100).toString() : "0"  
                + (this.getFullYear() % 100));   
  
        str = str.replace(/MM/, this.getMonth() + 1 > 9 ? (this.getMonth() + 1)   
                .toString() : "0" + (this.getMonth() + 1));   
        str = str.replace(/M/g, (this.getMonth() + 1).toString());   
  
        str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate()   
                .toString() : "0" + this.getDate());   
        str = str.replace(/d|D/g, this.getDate());   
  
        str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours()   
                .toString() : "0" + this.getHours());   
        str = str.replace(/h|H/g, this.getHours());   
  
        str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes()   
                .toString() : "0" + this.getMinutes());   
        str = str.replace(/m/g, this.getMinutes());   
  
        str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds()   
                .toString() : "0" + this.getSeconds());   
        str = str.replace(/s|S/g, this.getSeconds());   
  
        return str;   
  
    } else {   
        return this.toLocaleString();   
    }   
};   
  
/** Returns the number of days in the current month */  
Date.prototype.getMonthDays = function(month) {   
    var year = this.getFullYear();   
    if (typeof month == "undefined") {   
        month = this.getMonth();   
    }   
    if (((0 == (year % 4)) && ((0 != (year % 100)) || (0 == (year % 400))))   
            && month == 1) {   
        return 29;   
    } else {   
        return Date._MD[month];   
    }   
};   
  
/** Returns the number of day in the year. */  
Date.prototype.getDayOfYear = function() {   
    var now = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0,   
            0, 0);   
    var then = new Date(this.getFullYear(), 0, 0, 0, 0, 0);   
    var time = now - then;   
    return Math.floor(time / Date.DAY);   
};   
  
/** Returns the number of the week in year, as defined in ISO 8601. */  
Date.prototype.getWeekNumber = function() {   
    var d = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0,   
            0);   
    var DoW = d.getDay();   
    d.setDate(d.getDate() - (DoW + 6) % 7 + 3); // Nearest Thu   
    var ms = d.valueOf(); // GMT   
    d.setMonth(0);   
    d.setDate(4); // Thu in Week 1   
    return Math.round((ms - d.valueOf()) / (7 * 864e5)) + 1;   
};   
  
/** Checks date and time equality */  
Date.prototype.equalsTo = function(date) {   
    return ((this.getFullYear() == date.getFullYear())   
            && (this.getMonth() == date.getMonth())   
            && (this.getDate() == date.getDate())   
            && (this.getHours() == date.getHours()) && (this.getMinutes() == date   
            .getMinutes()));   
};  

//Just like Java StringBuilder
var StringBuilder = function(){
	this.array = [];
	
	// 添加
	this.append = function( value ){
		this.array.push(value);
		return this;
	};
	
	// 清空
	this.clear = function(){
		this.array = [];
	};
	
	// 长度
	this.length = function(){
		return this.array.length;
	};
	
	//转化为字符串
	this.toString = function(){
		this.array.join("");
	};
};

